
package com.viweb.storemanager.core;

import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;


public class ButtonCellEditor extends AbstractCellEditor implements TableCellEditor {

    /** The Swing component being edited. */
    private JButton editorComponent;
    private ImageIcon imageIcon;

    public ButtonCellEditor() {
        this.editorComponent = new JButton();
    }

    public ButtonCellEditor(Action action) {
        this.editorComponent = new JButton();
        this.editorComponent.setAction(action);
    }

    public ButtonCellEditor(Action action, ImageIcon imageIcon) {
        this.editorComponent = new JButton();
        this.imageIcon = imageIcon;
        this.editorComponent.setAction(action);
    }

    public Object getCellEditorValue() {
        return "*";
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        if (this.imageIcon == null) {
            this.editorComponent.setText((String) value);
        } else {
            this.editorComponent.setIcon(this.imageIcon);
            this.editorComponent.setToolTipText(this.imageIcon.getDescription());
        }

        return this.editorComponent;
    }
}
